﻿using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using Spring.Context.Support;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Application.MainBoundedContext.SystemModule
{
    public class BaseCodeRuleAppService : BaseAppService<BaseCodeRule, BaseCodeRuleSearch, IBaseCodeRuleRepository>, IBaseCodeRuleAppService
    {
        private ICodeRulePropertyAppService CodeRulePropertyAppService { get; set; }

        #region IBaseCodeRuleAppService Members

        public override BoolGenericResult<BaseCodeRule> AddOrUpdate(BaseCodeRule entity)
        {
            Init();
            BoolGenericResult<BaseCodeRule> result = new BoolGenericResult<BaseCodeRule>();
            if (entity.IsNull())
            {
                throw new ArgumentNullException("");
            }
            this.CodeRulePropertyAppService = this.SpringContext.GetObject(SystemModuleConst.CODERULEPROPERTYAPPSERVICE) as ICodeRulePropertyAppService;
            Employee employee = this.EmployeeAppService.GetBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id)).FirstOrDefault();
            Administrator admin = new Administrator(employee);

            BaseCodeRule oldBaseCodeRule = this.Repository.Get(entity.Id);
            CodeRuleProperty codeRuleProperty = null;
            if (oldBaseCodeRule.IsNull())
            {
                result = admin.AddBaseCodeRule(entity.Name, entity.Type, entity.Prefix, entity.IsDefault, entity.SerialNumberNumber, entity.SerialNumberStart,
                    entity.Example);
                if (result.IsSuccess)
                { 
                    base.Add(result.Object);
                    codeRuleProperty = ((EditBaseCodeRule)result).CodeRuleProperty;
                    codeRuleProperty.CodeRuleID = result.Object.Id;
                    this.CodeRulePropertyAppService.Add(((EditBaseCodeRule)result).CodeRuleProperty);
                }
            }
            else
            {
                CodeRulePropertySearch codeRulePropertySearch = SearchFactory.CreateCodeRulePropertySearch(oldBaseCodeRule.Id, SystemIdentifier.CodeRuleType.Base.GetHashCode());
                codeRuleProperty = this.CodeRulePropertyAppService.GetSingleBySearch(codeRulePropertySearch);
                result = admin.UpdateBaseCodeRule(oldBaseCodeRule, codeRuleProperty, entity.Name, entity.Type, entity.Prefix, entity.IsDefault, entity.SerialNumberNumber, entity.SerialNumberStart,
                    entity.Example);
                if (result.IsSuccess)
                {
                    base.Update(result.Object);
                    this.CodeRulePropertyAppService.Update(codeRuleProperty);
                }
                     
            }
            return result;
        }

        #endregion
    }
}
